package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.bo.ErpCollectProductPublishTaskBo;
import com.erp.gather.domain.vo.ErpCollectProductPublishTaskVo;
import com.erp.gather.mapper.ErpCollectProductMapper;
import com.erp.gather.mapper.ErpCollectProductPublishTaskMapper;
import com.erp.gather.service.IErpCollectProductPublishTaskService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpCollectProduct;
import com.ruoyi.gather.api.domain.ErpCollectProductPublishTask;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 产品库发布记录Service业务层处理
 *
 * @author erp
 * @date 2023-06-01
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductPublishTaskServiceImpl implements IErpCollectProductPublishTaskService {

    private final ErpCollectProductPublishTaskMapper baseMapper;
    private final ErpCollectProductMapper erpCollectProductMapper;

    /**
     * 查询产品库发布记录
     */
    @Override
    public ErpCollectProductPublishTaskVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询产品库发布记录列表
     */
    @Override
    public TableDataInfo<ErpCollectProductPublishTaskVo> queryPageList(ErpCollectProductPublishTaskBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductPublishTask> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductPublishTaskVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        result.getRecords().forEach(vo->{
            ErpCollectProduct product=erpCollectProductMapper.selectById(vo.getCollectProductId());
            if(product!=null){
                vo.setImageURLs(product.getImageURLs());
                vo.setSubject(product.getSubject());
                vo.setErpCollectProduct(product);
            }
        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询产品库发布记录列表
     */
    @Override
    public List<ErpCollectProductPublishTaskVo> queryList(ErpCollectProductPublishTaskBo bo) {
        LambdaQueryWrapper<ErpCollectProductPublishTask> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductPublishTask> buildQueryWrapper(ErpCollectProductPublishTaskBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductPublishTask> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCollectProductId() != null, ErpCollectProductPublishTask::getCollectProductId, bo.getCollectProductId());
        lqw.eq(bo.getStatus() != null, ErpCollectProductPublishTask::getStatus, bo.getStatus());
        lqw.eq(bo.getSellerId() != null, ErpCollectProductPublishTask::getSellerId, bo.getSellerId());
        lqw.eq(bo.getPublishDate() != null, ErpCollectProductPublishTask::getPublishDate, bo.getPublishDate());
        lqw.orderByDesc(ErpCollectProductPublishTask::getPublishDate);
        return lqw;
    }

    /**
     * 新增产品库发布记录
     */
    @Override
    public Boolean insertByBo(ErpCollectProductPublishTaskBo bo) {
        ErpCollectProductPublishTask add = BeanUtil.toBean(bo, ErpCollectProductPublishTask.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改产品库发布记录
     */
    @Override
    public Boolean updateByBo(ErpCollectProductPublishTaskBo bo) {
        ErpCollectProductPublishTask update = BeanUtil.toBean(bo, ErpCollectProductPublishTask.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductPublishTask entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除产品库发布记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    //创建新增记录
    @Override
    public ErpCollectProductPublishTask insertData(Long collectProductId, Long seellerId, Integer ststus, Date publishDate, String remark){
        ErpCollectProductPublishTask entity=new ErpCollectProductPublishTask();
        entity.setCollectProductId(collectProductId);
        entity.setSellerId(seellerId);
        entity.setStatus(ststus);
        entity.setPublishDate(publishDate);
        entity.setRemark(remark);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    public R<Object> getCollectProductPublishTaskOneOrderByCreateTime(String productId, Long userId) {
        ErpCollectProductPublishTask publishTask = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpCollectProductPublishTask>()
                        .eq(ErpCollectProductPublishTask::getCollectProductId, productId)
                        .eq(ErpCollectProductPublishTask::getSellerId, userId)
                        .orderByDesc(ErpCollectProductPublishTask::getCreateTime)
                        .last("limit 1")
        );
        if (Objects.isNull(publishTask)){
            return R.ok(null);
        }else {
            return R.ok(publishTask);
        }
    }


}
